<template>
  <div class="app-container">
    <div style="width: 100%; display: flex; justify-content: space-between; flex-direction: row;">
      <div>
        <el-radio-group v-model="collectionRedio" size="small" style="margin-left: 20px;" @change="queryList()">
          <el-radio-button label="全部"></el-radio-button>
          <el-radio-button label="收藏"></el-radio-button>
        </el-radio-group>

      </div>
      <div style="margin-left: 10px;">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >上传图片</el-button>
      </div>
    </div>

     <div>
        <ul class="ul_material table clearfix">
          <li v-for="(m,index) in materialList" :key="index">
            <el-image :src="m.url"
            style="width: 200px; height: 160px; border-radius: 20%"></el-image>
            <div style="text-align: center;">
              <el-link  :underline="false"  :icon=checkCollection(m.isCollection) style="margin-right: 20px;"
                        @click="handleUpdate(m.id,m.isCollection)">收藏</el-link>

              <el-link :underline="false" icon="el-icon-delete" @click="handleDelete(m.id)">删除</el-link></div>
          </li>
        </ul>
     </div>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改自媒体图文素材信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="图片地址" prop="url">
          <el-input v-model="form.url" placeholder="请输入图片地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改OSS对象存储对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="文件名">
          <imageUpload4News v-model="form.file"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMaterial, getMaterial, delMaterial, addMaterial, updateMaterial } from "@/api/wemedia/material";

export default {
  name: "Material",
  data() {
    return {
      collectionRedio: "全部",
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 自媒体图文素材信息表格数据
      materialList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        url: undefined,
        type: undefined,
        isCollection: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        url: [
        ]

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    queryList(){

      this.queryParams.isCollection = this.collectionRedio == "全部" ? undefined : 1;
      this.getList();
    },
    /** 查询自媒体图文素材信息列表 */
    getList() {
      this.loading = true;
      listMaterial(this.queryParams).then(response => {
        this.materialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    checkCollection(c){
      return c == 0? "el-icon-star-off":"el-icon-star-on";
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        url: undefined,
        type: undefined,
        isCollection: undefined,
        createTime: undefined,
        createBy: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加自媒体图文素材信息";
    },
    /** 修改按钮操作 */
    handleUpdate(id,isCollection) {
      isCollection = isCollection == 0 ? 1 :  0;
      updateMaterial({id:id,isCollection:isCollection}).then(response => {
        this.getList();
        this.$modal.msgSuccess("操作成功！");
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.open = false;
      this.getList();
    },
    handleDelete(id) {
      this.$modal.confirm('是否确认删除?').then(() => {
        this.loading = true;
        return delMaterial(id);
      }).then(response => {
        console.log(response);
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wemedia/material/export', {
        ...this.queryParams
      }, `material_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style>
.ul_material{
  padding:0;
  margin:0;
  list-style: none;
}
.table{
  width: 110%;
}
.table li{
  float: left;
  width: 201px;
  height: 161px;
  margin: 20px;
  box-sizing: border-box;
}
.clearfix::after{
  content:'';
  display: table;
  clear: both;
}
</style>
